package com.cai.dao.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cai.dao.DbUtil;
import com.cai.dao.ProcessInfoCenterDao;
import com.cai.pojo.Message;

import java.util.List;

/**
 * @author RuoLi
 */
public class ProcessInfoCenterDaoImpl implements ProcessInfoCenterDao {
    @Override
    public int sendProcessInfoMessage(int userAccount, String sendMessage) {
        int status;
        String sql = "INSERT INTO `online_quiz_website`.`message` (` message_context`, `user_account`) VALUES (?, ?)";
        status = DbUtil.updateContent(sql, new Object[]{sendMessage, userAccount});
        return status;
    }

    @Override
    @SuppressWarnings("unchecked")
    public String selectAllProcessInfoMessages() {
        String sql = "select * from message";
        DbUtil dbUtil = new DbUtil();
        List<Message> list = (List<Message>) dbUtil.selectContent(sql, new String[]{}, Message.class);
        System.out.println(list);
        JSONArray jsonArray = new JSONArray();
        for (Message message : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("messageId", message.getMessage_id());
            jsonObject.put("userAccount", message.getUser_account());
            jsonObject.put("status", message.getSettle_status());
            jsonObject.put("messageContext", "测试说一句话");
            jsonArray.add(jsonObject);
        }
        return jsonArray.toString();
    }

    @Override
    public int selectCountIsExit(int userAccount, int messageId) {
        int count;
        String sql = "select count(*) from message where user_account=?  and message_id=?";
        count = DbUtil.selectCountIsExist(sql, new String[]{String.valueOf(userAccount), String.valueOf(messageId)});
        return count;
    }

    @Override
    public int replyProcessInfoMessage(int userAccount, int messageId, String replyMessage) {
        int count;
        String sql="update message set message_reply=? where user_account=? and message_id=?";
        count= DbUtil.updateContent(sql,new Object[]{replyMessage,userAccount,messageId});
        return count;
    }

    @Override
    @SuppressWarnings("unchecked")
    public String selectAllReplyProcessInfoMessage() {
        String sql="select message_reply,user_account,message_id from message";
        DbUtil dbUtil=new DbUtil();
        List<Message> list=(List<Message>) dbUtil.selectContent(sql,new String[]{},Message.class);
        JSONArray jsonArray=new JSONArray();
        int count=0;
        for (Message message:list
             ) {
            ++count;
            JSONObject json=new JSONObject();
            json.put("replyMessage",message.getMessage_reply());
            json.put("userAccount",message.getUser_account());
            json.put("messageId",message.getMessage_id());

            if (message.getMessage_reply().length()==0){

            }
            jsonArray.add(json);
            jsonArray.remove(1);
        }
        return jsonArray.toString();
    }

}
